using MvvmFx.Common.ViewModels.Commands;

namespace MvvmFx.Samples.HowTos.PropertyRehandlers
{
    public class SetBoolCommand : ViewModelCommand
    {
        private readonly BoolViewModel _vm;
        private readonly bool _value;

        public SetBoolCommand(BoolViewModel vm, bool value)
        {
            _vm = vm;
            _value = value;

            vm.NewPropertyObserver().AddHandler("IsTrue",
                                                boolVM => RaiseCanExecuteChanged());

        }

        protected override void OnExecute(object parameter)
        {
            _vm.IsTrue = _value;
        }

        protected override bool OnCanExecute(object parameter)
        {
            return _vm.IsTrue != _value;
        }
    }
}